<template>
  <el-dialog
    title="编辑库存"
    :visible="value"
    width="500px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <el-form
      ref="form"
      :model="form"
      :inline="true"
      :rules="rules"
      label-width="90px"
      label-position="right"
      @submit.native.prevent="onSubmit"
    >
      <el-form-item label="药品名称：" style="width:100%">
        <label>{{form.goodsName}}</label>
      </el-form-item>
      <el-form-item label="供应商：" style="width:100%">
        <el-select
          v-model="form.supplierId"
          filterable
          :filter-method="getSupplier"
          clearable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="(item,index) in supplierList"
            :key="index"
            :value="item.id"
            :label="item.supplierName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="进价：" style="width:100%" prop="purchasePrice">
        <el-input v-model="form.purchasePrice"></el-input>
      </el-form-item>
       <el-form-item label="生产厂家：" style="width:100%" prop="factory">
        <el-input v-model="form.factory"></el-input>
      </el-form-item>
      <el-form-item label="批号：" style="width:100%">
        <el-input v-model="form.lotNumber"></el-input>
      </el-form-item>
      <el-form-item label="有效期：" style="width:100%">
        <el-date-picker v-model="form.effectiveTime" type="date" value-format="yyyy-MM-dd" placeholder="选择有效期"></el-date-picker>
      </el-form-item>
    <el-button v-show="false" :loading="btnLoading" native-type="submit" >提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Valid from '@/_js/validate'
export default {
  props: ["value", "curItem"],
  data() {
    return {
      rules: {
        purchasePrice:[ { required: true, message: "请输入价格", trigger: "blur" },{ validator: Valid.integerAndFloat,message:"请输入正确的进价", trigger: "blur" }],
        effectiveTime:[{ required: true, message: "请选择有效期", trigger: "blur" }]
      },
      form: {
        goodsName:'',
        purchasePrice: "", //进价
        lotNumber: "", //批号
        effectiveTime: "", //有效期
        supplierId: "",
        stockBatchId: "",
        factory:'',//生产厂家
      },
      btnLoading:false,
      supplierList:[]
    };
  },
  mounted() {
    const th=this;
    th.getSupplier();
  },
  methods: {
    onClose() {
      const th=this;
      th.$emit("input",false)
    },
    onSubmit(){
      const th=this;
      th.btnLoading=true;
      let j={}
      Object.assign(j,th.form)
      th.$api.editStockDetail(j).then(res=>{
         if(res.executed){
           th.$message.success(res.message);
           th.$emit("success");
           th.onClose();
         }else{
           th.$message.error(res.message);
         }
         th.btnLoading=false;
      }).catch(error=>{
        th.btnLoading=false;
      })
    },
    onOpen() {
      const th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.goodsName=th.curItem.goodsName;
          th.form.purchasePrice = th.curItem.purchasePrice;
          th.form.effectiveTime=th.curItem.effectiveTime;
          th.form.supplierId=th.curItem.supplierId;
          th.form.lotNumber=th.curItem.lotNumber;
          th.form.stockBatchId=th.curItem.id;
          th.form.factory=th.curItem.factory;
        }
      });
    },
    getSupplier(query) {
      let th = this;
      th.$api.findHpSupplierAll({ pageNum: 1, pageSize: 100, supplierName: query }).then(res => {
          if (res.executed) {
            th.supplierList = res.dataBody.list;
          }
        });
    }
  }
};
</script>